import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * 
 * @author Adam and Terry
 *
 */
public class MapMenu extends JMenuBar implements ActionListener {
	
	MapFrame map; 

	/**
	 * 
	 * @param map
	 */
	public MapMenu(MapFrame map) {
	
		this.map = map;
		// File menu
		JMenu file = new JMenu("File");
		file.setMnemonic(KeyEvent.VK_F);
		this.add(file);
		// File sub items
		JMenuItem loadFile = new JMenuItem("Load File", KeyEvent.VK_L);
		loadFile.addActionListener(this);
		file.add(loadFile);
		
		JMenuItem saveFile = new JMenuItem("Save File", KeyEvent.VK_S);
		saveFile.addActionListener(this);
		file.add(saveFile);
		// Load file 
		JMenuItem fileExit = new JMenuItem("Exit", KeyEvent.VK_X);
		fileExit.addActionListener(this);
		file.add(fileExit);
		// Help menu
		JMenu help = new JMenu("Help");
		help.setMnemonic(KeyEvent.VK_H);
		this.add(help);
		// File sub items
		JMenuItem helpAbout = new JMenuItem("About", KeyEvent.VK_A);
		helpAbout.addActionListener(this);
		help.add(helpAbout);
	}
	
	/**
	 * 
	 * @return the path to the file 
	 */
	public String file()
	{
		JFileChooser fc = new JFileChooser();
		 int returnVal = fc.showOpenDialog(this);

	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            //This is where a real application would open the file.
	            return file.getAbsolutePath();
	        } else {
	           return null;
	        }
	}

	/**
	 * show message about dialog box 
	 */
	private void about() {
		JOptionPane.showMessageDialog(this.getParent(), "Assignment 1 : Melbourne Map\nAdam Dickson :2358840\nTerry Miller : 6463630", "About", JOptionPane.INFORMATION_MESSAGE);
	}
	
	/**
	 * 
	 * @param message
	 * @param title
	 * @return true or false depending on user selection 
	 */
	
	public boolean displayChoiceDialogBox(String message, String title)
	{
		int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
          return true;
        }
        else {
           return false; 
        }

	}
	
	/**
	 * 
	 */

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equals("About"))
			this.about();
		if(arg0.getActionCommand().equals("Load File"))
			map.run(map, this.file());
		if(arg0.getActionCommand().equals("Save File")){
			
			if(map.getArrayListSize() != 0){
				if(displayChoiceDialogBox("Are You Sure You Want To Save?", "Save Points?")){
					map.savepointstofile();
				}
			}
		}
		if (arg0.getActionCommand().equals("Exit")){
			
			if(map.getArrayListSize() == 0)
			{
				System.exit(0);
			}
			else
			{
				if(displayChoiceDialogBox("You Have Some Unsaved Points Do You Want To Continue ? ", "Close Program?"))
					System.exit(0);
			}
		}
	}
}
